{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = '1'\n",
    "\n",
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import time\n",
    "\n",
    "from architecture import shufflenet\n",
    "\n",
    "# add path to this:\n",
    "# https://github.com/tensorflow/models\n",
    "import sys\n",
    "sys.path.append('../models/research/slim/')\n",
    "\n",
    "from nets.mobilenet_v1 import mobilenet_v1\n",
    "from nets.mobilenet.mobilenet_v2 import mobilenet"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create a graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tf.reset_default_graph()\n",
    "\n",
    "BATCH_SIZE = 8\n",
    "random_batch = tf.constant(np.random.randn(BATCH_SIZE, 224, 224, 3), dtype=tf.float32)\n",
    "\n",
    "# uncomment only one:\n",
    "# logits, _ = mobilenet_v1(random_batch, is_training=False, depth_multiplier=1.0)\n",
    "# logits, _ = mobilenet(random_batch, num_classes=1000, depth_multiplier=1.0)\n",
    "logits = shufflenet(random_batch, is_training=False, num_classes=1000, depth_multiplier='2.0')\n",
    "\n",
    "init = tf.global_variables_initializer()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Measure inference times"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "times = []\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for _ in range(1010):\n",
    "        start = time.perf_counter()\n",
    "        sess.run(logits)\n",
    "        times.append(time.perf_counter() - start)\n",
    "        \n",
    "times = np.array(times[10:])\n",
    "print('mean:', times.mean(), 'std:', times.std())\n",
    "\n",
    "# https://en.wikipedia.org/wiki/Taylor_expansions_for_the_moments_of_functions_of_random_variables\n",
    "mean = times.mean()/BATCH_SIZE\n",
    "std = times.std()/BATCH_SIZE\n",
    "images_per_second = (1/mean) + (1/(mean**3))*(std**2)\n",
    "images_per_second_std = std/(mean**2)\n",
    "\n",
    "print('images per second:', images_per_second, images_per_second_std)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
